package com.mr.cai.main.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cai.mr.lib.interImp.ErrorCode;
import com.cai.mr.lib.utils.LogInfo;
import com.mr.cai.main.R;
import com.mr.cai.main.utils.CommonCoreUtil;
import com.mr.cai.main.view.PublicLoadLayout;

/**
 * 所有Fragment的基类
 * Created by Administrator on 2016/1/19.
 */
public abstract class BaseFragment extends Fragment {
    private final static  String TAG=BaseFragment.class.getSimpleName();
    protected PublicLoadLayout mPublicLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    /**
     * 公用的错误加载页面
     * @param type 错误码
     * @param msg 错误描述
     * @param isFristLoad 是否是第一次加载 判断是使用Toast还是背景图
     */
    protected void showError(int type, String msg, boolean isFristLoad){

        switch (type){
            case ErrorCode.NETWORK_NOT_AVAILABLE:
                showNetError(isFristLoad);
                break;
            case ErrorCode.NETWORK_REQUEST_ERROR:

            case ErrorCode.DATA_REQUEST_NULL:

            case ErrorCode.JOSN_PARSER_ERROR:
                showDataError(isFristLoad);
                break;
        }
        LogInfo.log(TAG, msg);
    }


    /**
     * 公用的错误加载页面
     * @param type 错误码
     * @param msg 错误描述
     * @param isFristLoad 是否是第一次加载 判断是使用Toast还是背景图
     */
    protected void showErrorWithFlag(int type,String msg,boolean isFristLoad){
        switch (type){
            case ErrorCode.NETWORK_NOT_AVAILABLE:
                showNetErrorWithFlag(isFristLoad);
                break;
            case ErrorCode.NETWORK_REQUEST_ERROR:

            case ErrorCode.DATA_REQUEST_NULL:

            case ErrorCode.JOSN_PARSER_ERROR:
                showDataError(isFristLoad);
                break;
        }
        LogInfo.log(TAG, msg);
    }




    protected void finishPublicLayout(){
        if(mPublicLayout!=null){
            mPublicLayout.finish();
        }

    }
    protected void showDataError(boolean isFirstLoad){
        if(isFirstLoad){
            if(mPublicLayout!=null){
                mPublicLayout.dataError(getResources().getString(R.string.data_request_fail),false);
            }
        }else{
            CommonCoreUtil.showToast(getResources().getString(R.string.data_request_fail));
        }
    }

    protected void showDataErrorWithFlag(boolean isFirstLoad){
        if(isFirstLoad){
            if(mPublicLayout!=null){
                mPublicLayout.dataError(getResources().getString(R.string.data_request_fail),false);
            }
        }else{
            CommonCoreUtil.showToast(getResources().getString(R.string.data_request_fail));
        }
    }


    protected void showNetError(boolean isFristLoad){
        if(isFristLoad){
            if(mPublicLayout!=null){
                mPublicLayout.netError(false);
            }
        }else{
            CommonCoreUtil.showToast(getResources().getString(R.string
                    .public_loading_net_null_err_txt));
        }
    }


    protected void showNetErrorWithFlag(boolean isShowContent){
        if(isShowContent){
            if(mPublicLayout!=null){
                mPublicLayout.netError(isShowContent);
            }
        }else{
            CommonCoreUtil.showToast(getResources().getString(R.string
                    .public_loading_net_null_err_txt));
        }
    }


}
